﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using RTSoft.Modules;

namespace RTSoft.SoureCode.Control.ChiTietHD
{
    class chitiethdctrl
    {
        DataProvider provider = new DataProvider();
        SqlCommand cmd = new SqlCommand();
        String strSQL = "";

        public void getHeader(ref String errConnect, ref String errSQL, ref DataTable tbl)
        {
            provider.Connection(ref errConnect);
            try
            {
                tbl = new DataTable();
                strSQL = "select ROW_ID,ID_HD,a.ID_MATHANG as ID_MATHANG,TEN_MATHANG,SOLUONG_BAN,DONGIA,THANHTIEN from CHITIET_HD as a, HANGHOA as b Where 0 = 1";
                tbl = provider.LayDanhSach(strSQL);
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
            }
            finally
            {
                provider.Disconnect();
            }
        }

        public void getData(ref String errConnect, ref String errSQL, ref DataTable tbl)
        {
            provider.Connection(ref errConnect);
            try
            {
                tbl = new DataTable();
                strSQL = "select * from CHITIET_HD ";
                tbl = provider.LayDanhSach(strSQL);
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
            }
            finally
            {
                provider.Disconnect();
            }
        }

        public void getDataReport(ref String errConnect, ref String errSQL, ref DataTable tbl)
        {
            provider.Connection(ref errConnect);
            try
            {
                tbl = new DataTable();
                strSQL = "select ROW_NUMBER() over (order by (select 1)) as STT,* from CHITIET_HD a ,HangHoa b Where a.ID_MATHANG = b.ID_MATHANG and ID_HD = (select Max(ID_HD) from HOADON) ";
                tbl = provider.LayDanhSach(strSQL);
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
            }
            finally
            {
                provider.Disconnect();
            }
        }

        public void Insert(ref String errConnect, ref String errSQL, chitiethdinfo info)
        {
            cmd.Connection = provider.Connection(ref errConnect);
            if (errConnect != "")
            {
                return;
            }
            cmd.Transaction = provider.BeginTransaction();
            try
            {
                SqlParameter f = new SqlParameter();
                f.IsNullable = true;
                f.Value = System.DBNull.Value;
                strSQL = "Insert into CHITIET_HD";
                strSQL = strSQL + "(ID_HD,ID_MATHANG,SOLUONG_BAN,DONGIA,THANHTIEN)";
                strSQL = strSQL + " values";
                strSQL = strSQL + "(@ID_HD,@ID_MATHANG,@SOLUONG_BAN,@DONGIA,@THANHTIEN)";
                cmd.CommandText = strSQL;
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@ID_HD", info.ID_HD);
                cmd.Parameters.AddWithValue("@ID_MATHANG", info.ID_MATHANG);
                cmd.Parameters.AddWithValue("@SOLUONG_BAN", info.SOLUONG_BAN);
                cmd.Parameters.AddWithValue("@DONGIA", info.DONGIA);
                cmd.Parameters.AddWithValue("@THANHTIEN", info.THANHTIEN);
                cmd.ExecuteNonQuery();
                provider.CommitTransaction();
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
                provider.RollbackTransaction();
                //' Neu xay ra loi thi rollback lai
            }
            finally
            {
                //'Dong ket noi
                cmd.Connection.Close();
                cmd.Connection.Dispose();
            }

        }

        public void Update(ref String errConnect, ref String errSQL, chitiethdinfo info)
        {
            cmd.Connection = provider.Connection(ref errConnect);
            if (errConnect != "")
            {
                return;
            }
            cmd.Transaction = provider.BeginTransaction();
            try
            {
                SqlParameter f = new SqlParameter();
                f.IsNullable = true;
                f.Value = System.DBNull.Value;
                strSQL = "Update CHITIET_HD SET  ";
                strSQL = strSQL + "ID_HD=@ID_HD,ID_MATHANG=@ID_MATHANG,SOLUONG_BAN=@SOLUONG_BAN,DONGIA=@DONGIA,THANHTIEN=@THANHTIEN Where  ROW_ID = @ROW_ID";
                cmd.CommandText = strSQL;

                cmd.Parameters.AddWithValue("@ROW_ID", info.ROW_ID);
                cmd.Parameters.AddWithValue("@ID_HD", info.ID_HD);
                cmd.Parameters.AddWithValue("@ID_MATHANG", info.ID_MATHANG);
                cmd.Parameters.AddWithValue("@SOLUONG_BAN", info.SOLUONG_BAN);
                cmd.Parameters.AddWithValue("@DONGIA", info.DONGIA);
                cmd.Parameters.AddWithValue("@THANHTIEN", info.THANHTIEN);
                cmd.ExecuteNonQuery();
                provider.CommitTransaction();
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
                provider.RollbackTransaction();
                //' Neu xay ra loi thi rollback lai
            }
            finally
            {
                //'Dong ket noi
                cmd.Connection.Close();
                cmd.Connection.Dispose();
            }

        }

        public void UpdateQuantity(ref String errConnect, ref String errSQL, int id,int val)
        {
            cmd.Connection = provider.Connection(ref errConnect);
            if (errConnect != "")
            {
                return;
            }
            cmd.Transaction = provider.BeginTransaction();
            try
            {
                SqlParameter f = new SqlParameter();
                f.IsNullable = true;
                f.Value = System.DBNull.Value;
                strSQL = "Update HANGHOA SET  ";
                strSQL = strSQL + "SOLUONG_MIN= (select soluong_min from hanghoa where id_mathang = @ID) - @SOLUONG Where  ID_MATHANG = @ID";
                cmd.CommandText = strSQL;
                cmd.Parameters.AddWithValue("@ID",id);
                cmd.Parameters.AddWithValue("@SOLUONG", val);
                cmd.ExecuteNonQuery();
                provider.CommitTransaction();
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
                provider.RollbackTransaction();
                //' Neu xay ra loi thi rollback lai
            }
            finally
            {
                //'Dong ket noi
                cmd.Connection.Close();
                cmd.Connection.Dispose();
            }

        }
        public void Delete(ref String errConnect, ref String errSQL, int id)
        {
            cmd.Connection = provider.Connection(ref errConnect);
            if (errConnect != "")
            {
                return;
            }
            cmd.Transaction = provider.BeginTransaction();
            try
            {
                strSQL = "DELETE FROM CHITIET_HD WHERE ROW_ID=@RowID";
                cmd.Parameters.AddWithValue("@RowID", id);
                cmd.CommandText = strSQL;
                cmd.ExecuteNonQuery();
                provider.CommitTransaction();
                //' Thuc hien transaction 
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
                provider.RollbackTransaction();
                //' Neu xay ra loi thi rollback lai
            }
            finally
            {
                //'Dong ket noi
                cmd.Connection.Close();
                cmd.Connection.Dispose();
            }
        }
    }
}